﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Ann
{
		public class SignalGenerator
		{
			public const int FZero = 0x100;
			public const int FOne = 0x200;

			public static Signal[] GenerateSignal(string text)
			{
				ArrayList signals = new ArrayList();
				char[] chars = text.ToCharArray();
				foreach (char c in chars)
				{
					signals.AddRange(GenerateSignal(c));
				}
				return (Signal[])signals.ToArray(typeof(Signal));
			}
			public static Signal[] GenerateSignal(char c)
			{
				char mask = (char)0x80;
				Signal[] cs = new Signal[8];
				for (int i = 7; i >=0; i--)
				{
					cs[i] = ((c & mask) == 0) ? Signal.of(FOne) : Signal.of(FZero);

					mask >>= 1;

				}
				return cs;
			}
		}

		public class Signal
		{
			public static Signal of(int frequency)
			{
				return new Signal(frequency);
			}

			public int frequency;
			public Signal(int frequency)
			{
				this.frequency = frequency;
			}

		}

		public class IonPool
		{
			public int IonTank = 0x1000;

			public int PullIons(int quant)
			{
				return (IonTank>=0)?( IonTank - quant):0;
			}
		}

		public class Ann
		{
			public const int IonThreshold = 0x1000;

			public IonPool IonPool = null;

			public int Ions = 0;

			public ArrayList AnnFriends = new ArrayList();

			public IDictionary  AnnWeights = new Hashtable ();

			public Ann(IonPool pool)
			{
				this.IonPool = pool;
			}

			public void Register(Ann ann,int weight)
			{
				this.AnnWeights[ann] = weight;
			}

			public void Connect(Ann ann,int weight)
			{
				this.AnnFriends.Add(ann);
				ann.Register(this, weight);
			}

			public void Accept(Signal s)
			{
				Accept(null, s);
			}

			public void Accept(Ann ann,Signal s)
			{
				int weight = (ann == null)? 1: (int)this.AnnWeights[ann];

				this.Ions+=this.IonPool.PullIons(weight * s.frequency);
				if (this.Ions>IonThreshold )
				{
					//EmitPulse(mysignal);
				}

			}

			public void EmitPulse(Signal s)
			{
				foreach (Ann ann in this.AnnFriends)
				{
					ann.Accept(this, s);
				}
			}

		}

		//public AnnForm()
		//{
		//    InitializeComponent();

		//    IonPool pool = new IonPool();

		//    Ann[] Anns = new Ann[10];

		//    for (int i = 0; i < 10; i++)
		//    {
		//        Anns[i] = new Ann(pool);
		//    }


		//    for (int i = 0; i < 9; i++)
		//    {
		//        Anns[i].Connect(Anns[9], 2);
		//    }

		//    Signal[] cs = SignalGenerator.GenerateSignal("Hello");

		//    foreach (Signal s in cs)
		//    {
		//        Anns[9].Accept(s);
		//    }

		//}

}
